﻿# Импорт объектов
from mydb.odbc import atable as odbctable
from mydb.mysql import atable as mysqltable


# Создание класса-манипулятора для объекта экспорта
class object:
	#This is a manipultor for export object
	#beginning of class suite
	def __init__(self, name, fields, chars=[], clause=[], sql='', dsn='1sbdb05'):
		self.dsn=dsn
		self.name=name
		self.primary=['mysql_id','integer','not null','']
		self.fields=[self.primary]+fields
		self.sql=sql
		self.chars=chars
		self.comment='Копия таблицы 1С77 '+self.name
		self.clause=clause
		self.odbctable=odbctable()
		self.odbctable.table.base.dsn=self.dsn
		self.odbctable.name=self.name
		self.mysqltable=mysqltable()
		self.mysqltable.name='1s_'+self.name
		self.mysqltable.fields=self.fields
		self.mysqltable.primary=self.primary[0]
		self.mysqltable.comment=self.comment
	
	def columns(self):
		columns=[]
		for list in self.fields[1:]:
			columns+=[list[0]]
		return columns

	def charsindex(self):
		columns=self.columns()
		result=[]
		for item in range(len(columns)):
			if columns[item] in self.chars:
				result+=[item]
		return result

	def extract(self):
		'Function creates generator of rows for columns from the table where clause is satisfied or for sql'
		if self.sql=='':
			result=self.odbctable.generate(self.columns(), self.clause)
		else:
			result=self.odbctable.table.base.generate(self.sql)
		return result
		
	def transformrows(self, rows):
		'Function decode chars in rows'
		length=len(self.columns())
		charsindex=self.charsindex()
		result=[]
		for row in rows:
			temp=[]
			for x in range(length):
				if x in charsindex:
					temp.append(row[x].decode('cp1251'))
				else:
					temp.append(row[x])
			result.append(temp)
		return result
		
	def transform(self, row, length, charsindex):
		'Function decode chars in rows'
		result=[]
		for x in range(length):
			if x in charsindex:
				result.append(row[x].decode('cp1251'))
			else:
				result.append(row[x])
		return result
	
	def updatetable(self, lim):
		'Function updates table in mysql'
		result=[self.delete(),0]
		gen=self.extract()
		length=len(self.columns())
		charsindex=self.charsindex()
		while True:
			rows=[]
			counter=0
			for row in gen:
				rows.append(self.transform(row, length, charsindex))
				counter+=1
				if counter==lim:
					break
			result[1]+=self.insert(self.columns(), rows)
			if counter<lim:
				break
		return result
	
	def create(self):
		'Function creates the table in mysql'
		return self.mysqltable.create()
		
	def drop(self):
		'Function creates the table in mysql'
		return self.mysqltable.drop()
	
	def countself(self):
		'Function counts tables having name of the table'
		return self.mysqltable.countself()
	
	def select(self, columns=[], clause=[], lim=0):
		'Function selects rows in the table'
		return self.mysqltable.select(columns, clause, lim)
	
	def generate(self, columns=[], clause=[], lim=1000):
		'Function selects rows in the table'
		return self.mysqltable.generate(columns, clause, lim)
	
	def iterate(self, columns=[], clause=[], start=None, stop=None, step=None):
		'Function selects rows in the table'
		return self.mysqltable.iterate(columns, clause, start, stop, step)

	def count(self, clause=[]):
		'Function selects rows in the table'
		return self.mysqltable.count(clause)

	def delete(self, clause=[]):
		'Function deletes from the table all records'
		return self.mysqltable.delete(clause)
	
	def insert(self, columns, values):
		'Function inserts rows in mysql table'
		return self.mysqltable.insert(columns, values)
	
	def update(self, dict, clause=[]):
		return self.mysqltable.update(dict, clause)

	#end of class object suite